import gc
import time
from pynput import mouse
from pynput.mouse import Button, Controller
from pynput import keyboard
from pynput.keyboard import Key, Controller
import time
import random

from threading import Thread, Lock
import threading

lock = Lock()
l = []
sem = threading.Semaphore(2)


def work():
    with sem:
        strUuid = random.randint(1, 50000)
        print('step 1:' + str(strUuid))

        lock.acquire()
        print('step 2:' + str(strUuid))

        #time.sleep(5)

        print('step 3:' + str(strUuid))
        lock.release()
        print('step 4:' + str(strUuid))
        # if strUuid > 10000:
        #     print("释放了一次内存")
        #     gc.collect()


# 控制鼠标
# mouse = Controller()
# #打印当前鼠标位置
# print('current pointer position {0}'.format(mouse.position))
# #鼠标从当前位置移动单位位置
# mouse.move(-100, -100)
# #按下右键
# mouse.press(Button.right)
# #释放右键
# mouse.release(Button.right)

# 监听鼠标
def on_move(x, y):
    # i = random.randint(3, 5)
    #
    # for i in range(0, 10000000):
    #     pass
    # print(str(i) + ':开始')
    # # time.sleep(i)
    # print(str(i) + ':Pointer moved in inner to {0}'.format((x, y)))
    p = Thread(target=work)
    l.append(p)
    p.start()
    # p.join()


def on_click(x, y, button, pressed):
    print('{0} at {1}'.format(
        'Pressed' if pressed else 'Released',
        (x, y)))
    # if not pressed:
    #     # Stop listener
    #     return False


def on_scroll(x, y, dx, dy):
    print('Scrolled {0} at {1}'.format(
        'down' if dy < 0 else 'up',
        (x, y)))


# Collect events until released
with mouse.Listener(
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = mouse.Listener(
    on_move=on_move,
    on_click=on_click,
    on_scroll=on_scroll)
listener.start()


# 键盘控制
# keyboard = Controller()
# time.sleep(3)
# #输入空格，小写a 大写A，以及小写时按shift+a输出的大写A
# keyboard.press(Key.space)
# keyboard.release(Key.space)
#
# keyboard.press('a')
# keyboard.release('a')
# keyboard.press('A')
# keyboard.release('A')
# with keyboard.pressed(Key.shift):
#     keyboard.press('a')
#     keyboard.release('a')
# #用键盘直接输出字符点击操作
# # Type 'Hello World' using the shortcut type method
# keyboard.type('Hello World')

# 键盘监听
def on_press(key):
    try:
        if key == Key.enter:
            print('you press Enter')
        else:
            return False  # 按键不是enter,停止监视
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))


def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False


# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
keyboard_listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
keyboard_listener.start()
